<?php

namespace App\Http\Controllers\Admin;


use App\Models\Map;
use App\Models\MapData;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;

/**
 * 分类管理
 * Class MapController
 * @package App\Http\Controllers\Admin
 */
class MapController extends BaseController
{
    /**
     * 分类列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {

        $list = Map::paginate(config('site.other.paginate'));
        return backendView('map.index', compact('list'));
    }

    /**
     * 新增分类
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function add(Request $request)
    {
        if ($request->isMethod('POST')) {

            $this->validate($request, [
                'name' => 'required|max:255|unique:maps',
                'title' => 'required|max:255',
                'is_system' => 'required|integer|in:0,1',
                'remark'=>'nullable|string'
            ], [], [
                'name' => '字典值',
                'title' => '字典标题',
                'is_system' => "是否系统参数",
                'remark'=>'备注'
            ]);

            $data = $request->only(['name','title','is_system', 'remark']);

            if (Map::create($data)) {
                return redirect()->route('admin.map.index')->with('success', __('Create success'));
            }

            return back()->with('error', __('Create failed'))->withInput();
        } else {
            return backendView('map.add');
        }
    }

    /**
     * 编辑分类
     * @param Request $request
     * @param null $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function edit(Request $request, $id = null)
    {
        /**
         * @var $map Map
         */
        $map = Map::find($id);
        if (empty($map)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }

        if ($request->isMethod('POST')) {

            $this->validate($request, [
                'name' => ['required', 'max:255', Rule::unique('maps')->ignore($id),],
                'title' => 'required|max:255',
                'is_system' => 'required|integer|in:0,1',
                'remark'=>'nullable|string'
            ], [], [
                'name' => '字典值',
                'title' => '字典标题',
                'is_system' => '是否系统参数',
                'remark'=>'备注'
            ]);

            $data = $request->only(['name','title','is_system', 'remark']);

            if ($map->update($data)) {
                return redirect()->route('admin.map.index')->with('success', __('Edit success'));
            }

            return back()->with('error', __('Edit failed'))->withInput();

        } else {
            return backendView('map.edit', compact('map'));
        }
    }
    /**
     * 删除分类
     * @param null $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function delete($id = null)
    {
        /**
         * @var $map Map
         */
        $map = Map::find($id);
        if (empty($map)) {
            return back()->with('error', __('The page you requested was not found'));
        }

        try {
            if ($map->is_system == 1){
                return back()->with('error', '系统参数不能删除');
            }

            $mapData = $map->mapData()->get();

            if (count($mapData) > 0) {
                return back()->with('error', '字典下有数据不能删除');
            }

            if ($map->delete()) {
                return back()->with('success', __('Delete success'));
            }
        } catch (\Exception $e) {
            return back()->with('error', $e->getMessage());
        }

        return back()->with('error', __('Delete failed'));
    }

    /**
     * 查看分类
     * @param null $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function data($id = null)
    {
        /**
         * @var $map Map
         */
        $map = Map::find($id);
        if (empty($map)) {
            return back()->with('error', __('The page you requested was not found'));
        }

        $mapData = $map->mapData;
        return backendView('map.data', compact('map', 'mapData'));
    }
    /**
     * 查看分类
     * @param null $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function add_data($map_id = null, Request $request)
    {
        /**
         *
         *
         * @var $map Map
         */
        $map = Map::find($map_id);
        if (empty($map)) {
            return back()->with('error', __('The page you requested was not found'));
        }

        if ($request->isMethod('POST')) {
            $this->validate($request, [
                'value' => 'required|max:255',
                'title' => 'required|max:255',
                'sort' => 'required|integer'
            ], [], [
                'value' => '字典值',
                'title' => '名称',
                'sort' => __("Sort")
            ]);

            $data = $request->only(['value','title','sort']);

            if ($map->mapData()->create($data)) {
                return redirect()->route('admin.map.data', ['id'=>$map_id])->with('success', __('Create success'));
            }

            return back()->with('error', __('Create failed'))->withInput();
        } else {
            $mapData = $map->mapData;

            return backendView('map.add_data', compact('map', 'mapData'));
        }


    }
    /**
     * 查看分类
     * @param null $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function edit_data($id = null, Request $request)
    {
        /**
         * @var $mapData MapData
         */
        $mapData = MapData::find($id);
        if (empty($mapData)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }

        if ($request->isMethod('POST')) {
            $this->validate($request, [
                'value' => ['required', 'max:255'],
                'title' => 'required|max:255',
                'sort' => 'required|integer'
            ], [], [
                'value' => '字典值',
                'title' => '名称',
                'sort' => __("Sort")
            ]);

            $data = $request->only(['value','title','sort']);

            if ($mapData->update($data)) {
                return redirect()->route('admin.map.data', ['id'=>$mapData->map_id])->with('success', __('Edit success'));
            }

            return back()->with('error', __('Edit failed'))->withInput();

        } else {
            return backendView('map.edit_data', compact('mapData'));
        }
    }

    /**
     * 查看分类
     * @param null $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function delete_data($id = null)
    {
        /**
         * @var $map Map
         */
        $mapData= MapData::find($id);
        if (empty($mapData)) {
            return back()->with('error', __('The page you requested was not found'));
        }

        try {
/*            $mapData = $map->mapData()->get();

            if (count($mapData) > 0) {
                return back()->with('error', '字典下有数据不能删除');
            }*/

            if ($mapData->delete()) {
                return back()->with('success', __('Delete success'));
            }
        } catch (\Exception $e) {
            return back()->with('error', $e->getMessage());
        }

        return back()->with('error', __('Delete failed'));
    }
}